/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biz;

import da.ReplyJpaController;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Reply;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.transaction.UserTransaction;

/**
 *
 * @author THOHCGC00177
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class ReplyProgess {
   
    @Resource
    private UserTransaction utx;
    @PersistenceUnit(unitName = "ProjectSem4PU")
    private EntityManagerFactory emf = null;
    private ReplyJpaController jpa;

    public ReplyJpaController getJpa() {
        if (jpa == null) {
            jpa = new ReplyJpaController(utx, emf);
        }
        return jpa;
    }
    /**
     * 
     * Function create Reply (Technician answer the complain of employee)
     * @param reply
     * @throws PreexistingEntityException
     * @throws RollbackFailureException
     * @throws Exception 
     */
    public void createReply(Reply reply) throws PreexistingEntityException, RollbackFailureException, Exception {
        getJpa().create(reply);
    } 
    /**
     * 
     * Function get list Reply by id
     * @param id
     * @return 
     */
    public Reply getReplyById(int id) {
        TypedQuery<Reply> tq = emf.createEntityManager().createQuery("SELECT d FROM Reply d WHERE d.complainId.id = :id", Reply.class);
        tq.setParameter("id", id);
        try {
            return tq.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    public List<Reply> getListReplyById(int id) {
        TypedQuery<Reply> tq = emf.createEntityManager().createQuery("SELECT d FROM Reply d WHERE d.complainId.id = :id", Reply.class);
        tq.setParameter("id", id);
        try {
            return tq.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
